/**
 * Copyright (C) 2016 Hyphenate Inc. All rights reserved.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ukao.cashregister.consts;


import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.EditText;
import android.widget.ScrollView;

import com.ukao.cashregister.R;
import com.ukao.cashregister.bean.AccountLoginBean;
import com.ukao.cashregister.retrofit.SignTool;
import com.ukao.cashregister.ui.MainActivity;
import com.ukao.cashregister.utils.CheckUtils;
import com.ukao.cashregister.utils.Utils;

import java.util.HashMap;

import me.yokeyword.fragmentation.SupportActivity;

import static com.ukao.cashregister.utils.CipherUtils.md5;

public class Constant {


    /* 微信   现金   余额 支付宝 银联    */
    public static final int[] VORDIPLOM_COLOR = {Color.rgb(94, 201, 124)
            , Color.rgb(243, 86, 80), Color.rgb(112, 79, 185), Color.rgb(90, 154, 239),
            Color.rgb(254, 134, 51), Color.rgb(217, 80, 138), Color.rgb(254, 149, 7), Color.rgb(254, 247, 120),
            Color.rgb(106, 167, 134), Color.rgb(53, 194, 209)

    };
    /* 微信   现金    支付宝 银联    */
    public static final int[] VORDIPLOM_COLOR1 = {Color.rgb(94, 201, 124)
            , Color.rgb(243, 86, 80), Color.rgb(90, 154, 239),
            Color.rgb(254, 134, 51), Color.rgb(112, 79, 185), Color.rgb(254, 149, 7), Color.rgb(254, 247, 120),
            Color.rgb(106, 167, 134), Color.rgb(53, 194, 209)

    };


    public static void scrollToView(final ScrollView scrollViewParent, final View view) {
        // Get deepChild Offset
        Point childOffset = new Point();
        getDeepChildOffset(scrollViewParent, view.getParent(), view, childOffset);
        // Scroll to child.
        scrollViewParent.smoothScrollTo(0, childOffset.y);
    }

    /**
     * Used to get deep child offset.
     * <p/>
     * 1. We need to scroll to child in scrollview, but the child may not the direct child to scrollview.
     * 2. So to get correct child position to scroll, we need to iterate through all of its parent views till the main parent.
     *
     * @param mainParent        Main Top parent.
     * @param parent            Parent.
     * @param child             Child.
     * @param accumulatedOffset Accumalated Offset.
     */
    public static void getDeepChildOffset(final ViewGroup mainParent, final ViewParent parent, final View child, final Point accumulatedOffset) {
        ViewGroup parentGroup = (ViewGroup) parent;
        accumulatedOffset.x += child.getLeft();
        accumulatedOffset.y += child.getTop();
        if (parentGroup.equals(mainParent)) {
            return;
        }
        getDeepChildOffset(mainParent, parentGroup.getParent(), parentGroup, accumulatedOffset);
    }


    /**
     * 默认状态下的本地图片
     */
    public static int[] mHomeSelectIcons = {
            R.drawable.home_order,
            R.drawable.home_clothing,
            R.drawable.home_store,
            R.drawable.home_factory,
            R.drawable.home_vip,
            R.drawable.home_order_query,
            R.drawable.home_clothing_query,
            R.drawable.home_setting,
            R.drawable.home_storage,
//            R.drawable.home_un_toorder,
            R.drawable.home_pack,
            R.drawable.home_statistics,
            R.drawable.home_price
    };


    /**
     * 按下状态下的本地图片
     */
    public static int[] mUnHomeSelectIcons = {
            R.drawable.home_un_order,
            R.drawable.home_un_cloting,
            R.drawable.home_un_store,
            R.drawable.home_un_factory,
            R.drawable.home_un_vip,
            R.drawable.home_un_order_query,
            R.drawable.home_un_clothing_query,
            R.drawable.home_un_setting,

            R.drawable.home_un_storage,
//            R.drawable.home_toorder,
            R.drawable.home_un_pack,
            R.drawable.home_un_statistics,
    };

/*		R.drawable.home_un_price,*/


    /**
     * 参数加密
     */
    public static HashMap<String, String> createParameter(HashMap<String, String> newparams) {
        //String  code="29479864";  /*麓云店  账号  wfj@xxd */
        //String  code="28548992";/*王府井店 账号   	wfj@xxd*/
//		String  code="77322298"; /*我自己本地*/

        String code;
        if (CheckUtils.isOfficialServerUrl()) {
            code = SaveParamets.getCode();/*正式打包*/
        } else {
            //code="29479864";  /*麓云店  账号  wfj@xxd */
            //code="28548992";/*王府井店 账号   	wfj@xxd*/
            code = "50063011"; /*我自己本地*/
        }
        String appSecret = SaveParamets.getsaveloginSign(Utils.getContext());
        String timestamp = System.currentTimeMillis() + "";
        newparams.put("serialNum", code);
        newparams.put("appSecret", appSecret);
        newparams.put("timestamp", timestamp);
        String sign = SignTool.sign(newparams);
        newparams.remove("appSecret");
        newparams.put("sign", sign);
        return newparams;
    }

    /**
     * 初始化ji加密参数  40307705/22271311
     */
    public static HashMap<String, String> firstParameter(HashMap<String, String> newparams) {
        String code;
        if (CheckUtils.isOfficialServerUrl()) {
            code = SaveParamets.getCode();/*正式打包*/
        } else {
            //code="29479864";  /*麓云店  账号  wfj@xxd */
            //code="28548992";/*王府井店 账号   	wfj@xxd*/
            code = "50063011"; /*我自己本地*/
        }
        String timestamp = System.currentTimeMillis() + "";
        newparams.put("serialNum", code);
        newparams.put("timestamp", timestamp);
        String token = md5(md5(code) + timestamp);
        newparams.put("token", token);
        return newparams;
    }

    public static void calculate(String s, EditText mNameEt) {
        if (s.contains(".")) {
            if (s.length() - 1 - s.toString().indexOf(".") > 2) {
                s = (String) s.subSequence(0,
                        s.indexOf(".") + 3);
                mNameEt.setText(s);
                mNameEt.setSelection(s.length());
            }
        }
        if (s.trim().substring(0).equals(".")) {
            s = "0" + s;
            mNameEt.setText(s);
            mNameEt.setSelection(2);
        }

        if (s.startsWith("0")
                && s.toString().trim().length() > 1) {
            if (!s.toString().substring(1, 2).equals(".")) {
                mNameEt.setText(s.subSequence(0, 1));
                mNameEt.setSelection(1);
                return;
            }
        }
    }


    public static Bitmap createDrawable(String letter, Bitmap mBitmap) {
        int width = mBitmap.getWidth();
        int height = mBitmap.getHeight();
        Bitmap imgTemp = Bitmap.createBitmap(mBitmap.getWidth(), mBitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(imgTemp);
        Paint paint = new Paint(); // 建立画笔
        paint.setDither(true);
        paint.setFilterBitmap(true);
        Rect src = new Rect(0, 0, width, height);
        Rect dst = new Rect(0, 0, width, height);
        canvas.drawBitmap(mBitmap, src, dst, paint);

        Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG
                | Paint.DEV_KERN_TEXT_FLAG);
        textPaint.setTextSize(20.0f);
        textPaint.setTypeface(Typeface.DEFAULT_BOLD); // 采用默认的宽度
        textPaint.setColor(Color.WHITE);

        canvas.drawText(String.valueOf(letter), width / 2 - 5, height / 2 + 5,
                textPaint);
        canvas.save(Canvas.ALL_SAVE_FLAG);
        canvas.restore();
        /*new BitmapDrawable(Utils.getContext().getResources(), imgTemp);*/
        return imgTemp;

    }

    public static Intent loginSucceedJump(SupportActivity _mActivity, AccountLoginBean mitem) {
        AccountLoginBean.WorkerInfoBean mworkerInfo = mitem.getWorkerInfo();
        AccountLoginBean.StoreInfoBean storeInfo = mitem.getStoreInfo();

        /*店铺*/
        SaveParamets.setStoreName(storeInfo.getName());
        SaveParamets.setCreateTimep(storeInfo.getCreateTime() + "");
        SaveParamets.setRegionalDesc(storeInfo.getRegionalDesc());
        SaveParamets.setAddrDesc(storeInfo.getAddrDesc());

        /*用户信息*/
        SaveParamets.setStoreId(mitem.getStoreId());

        //商户
        SaveParamets.setMercInfo(mitem.getMercInfo().getName());
        SaveParamets.setMercLogoPath(mitem.getMercInfo().getLogoPath());
        SaveParamets.setMercMainInfo(mitem.getMercInfo().getMainInfo());

        SaveParamets.saveToken(_mActivity, mitem.getAccessToken());
        SaveParamets.saveUseName(_mActivity, mworkerInfo.getName());
        SaveParamets.saveUsePhone(_mActivity, mworkerInfo.getPhone());
        SaveParamets.saveHeadimgPath(_mActivity, mworkerInfo.getImgPath());

        SaveParamets.setIslogin(true);
        Intent mIntent = new Intent(_mActivity, MainActivity.class);

        return mIntent;
    }


}



